﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace SvnNotes
{
    public class OutputTransformerFactory
    {
        public static IOutputTransformer GetOutputTransformer(string assemblyFileName, string typeName)
        {
            IOutputTransformer transformer = null;
            if (String.IsNullOrEmpty(assemblyFileName)) throw new ArgumentNullException("assemblyFileName");
            if (String.IsNullOrEmpty(typeName)) throw new ArgumentNullException( "typeName");

            string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            assemblyFileName = dir + "\\" + assemblyFileName;

            Assembly assembly = Assembly.LoadFile(assemblyFileName);
            Type type = assembly.GetType(typeName, true, true);


            if (typeof(IOutputTransformer).IsAssignableFrom(type))
            {
                transformer = (IOutputTransformer)Activator.CreateInstance(type);
            }
            else
            {
                throw new InvalidProgramException(String.Format("{0} does not implement {1}", type.Name, typeof(IOutputTransformer).Name));
            }
            return transformer;
        }
    }
}
